package com.gxuwz.Market.business.action.web;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.Gson;
import com.gxuwz.Market.SystemContext;
import com.gxuwz.Market.business.entity.ServiceAcademy;
import com.gxuwz.Market.business.entity.ServiceClass;
import com.gxuwz.Market.business.entity.ServiceField;
import com.gxuwz.Market.business.entity.ServiceMajor;
import com.gxuwz.Market.business.entity.ServiceStudio;
import com.gxuwz.Market.business.entity.SysRole;
import com.gxuwz.Market.business.entity.SysUser;
import com.gxuwz.Market.business.entity.SysUserRole;
import com.gxuwz.Market.business.service.IServiceAcademyService;
import com.gxuwz.Market.business.service.IServiceClassService;
import com.gxuwz.Market.business.service.IServiceMajorService;
import com.gxuwz.Market.business.service.IServiceStudioService;
import com.gxuwz.Market.business.service.ISysRoleService;
import com.gxuwz.Market.business.service.ISysUserRoleService;
import com.gxuwz.Market.business.service.ISysUserService;
import com.gxuwz.core.pagination.Result;
import com.gxuwz.core.web.action.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class SysUserAction extends BaseAction implements Preparable, ModelDriven<SysUser> {

	protected static final String ADD_JSP = "/WEB-INF/page/web/SysUser_add.jsp";
	protected static final String EDIT_JSP = "/WEB-INF/page/web/SysUser_edit.jsp";
	protected static final String EDIT_SELF_JSP = "/WEB-INF/page/web/SysUser_edit_self.jsp";
	protected static final String DETAILS_JSP = "/WEB-INF/page/web/SysUser_details.jsp";
	protected static final String AllUser_JSP = "/WEB-INF/page/web/SysUser_allUsers.jsp";
	protected static final String ADD_STU_JSP= "/WEB-INF/page/web/addStudent.jsp";  
	protected static final String ADD_TEA_JSP= "/WEB-INF/page/web/Service_addTeacher.jsp"; 
	protected static final String LIST_JSP= "/WEB-INF/page/web/Service_userList.jsp";
	protected static final String INFO_JSP= "/WEB-INF/page/web/PersonInfo.jsp"; 
	
	
	@Autowired
	private ISysUserService sysUserService;
	@Autowired
	private ISysUserRoleService sysUserRoleService;
	@Autowired
	private ISysRoleService sysRoleService;
	@Autowired
	private IServiceStudioService serviceStudioService;
	@Autowired
	private IServiceMajorService serviceMajorService;
	@Autowired
	private IServiceAcademyService serviceAcademyService;
	@Autowired
	private IServiceClassService serviceClassService;
	
	private SysUser sysUser;
	/*private Result<SysUser> pageResult;*/
	private List<ServiceMajor> majorList;
	public List<ServiceMajor> getMajorList() {
		return majorList;
	}

	public void setMajorList(List<ServiceMajor> majorList) {
		this.majorList = majorList;
	}

	private List<ServiceField> fieldList;
	private List<ServiceAcademy>academyList;//学院列表
	private List<ServiceMajor>SerMajor;//专业列表
	private List<ServiceClass>SerCalss;//班级列表
	private List<ServiceStudio>SerStusdio;//研究室列表
	private Result pageResult;
	private ServiceAcademy academy;//院系实体
	private  ServiceMajor Major;//专业实体
	private ServiceClass cla;//班级实体
	private ServiceStudio studio;
	private String msg;//是否修改成功
	private List<SysRole> roles; //角色
	private List<SysUser> SysUserList;//用户列表
	private String AcaVar;
	private String MajVar;
	private String ClaVar;
	private String StudioVar;
	private List<SysUserRole> userRoles;//接收用户角色列表
	private String oldPassword;           //用户原来的密码
	private SysUserRole sysUserRole; //用户角色模型
	private String[] roleId;
	private String userid;//选中用户id
	
	
	
	
	
	
	public String getAcaVar() {
		return AcaVar;
	}

	public void setAcaVar(String acaVar) {
		AcaVar = acaVar;
	}

	public String getMajVar() {
		return MajVar;
	}

	public void setMajVar(String majVar) {
		MajVar = majVar;
	}

	public String getClaVar() {
		return ClaVar;
	}

	public void setClaVar(String claVar) {
		ClaVar = claVar;
	}

	public String getStudioVar() {
		return StudioVar;
	}

	public void setStudioVar(String studioVar) {
		StudioVar = studioVar;
	}

	@Override
	public void prepare() throws Exception {
		if(null == sysUser){
			sysUser = new SysUser();
		}
		if(null == academy){
			academy = new ServiceAcademy();
		}
		
		if(null == Major){
			Major = new ServiceMajor();
		}
	}

	@Override
	public SysUser getModel() {
		return sysUser;
	}
	
	@Override
	public String execute() throws Exception {
		
		return SUCCESS;
	}
	
	
	
/**
	 *打开添加学生用户界面
	 * @author 黄森南
	 * @date 2018-1-25
	 * @return
	 */
public String  openaddStu()
 {
	/*
	 * 查询所有的院系
	 */
	academyList= sysUserService.findAllAcademy();
	forwardView=ADD_STU_JSP;
	return SUCCESS;
 }
/**
 *打开添加教师用户界面
 * @author 黄森南
 * @date 2018-1-25
 * @return
 */
public String  openaddTea()
{
	SerStusdio =sysUserService.findStudio();
	forwardView =ADD_TEA_JSP;
	return SUCCESS;
	
}

/**
 *异步请求过院系的Id查询专业
 * @author 黄森南
 * @date 2018-1-25
 * @return
 */
public String MajortoJson()
{
	
    SerMajor= sysUserService.findByAca_Id(sysUser.getUserCollege());
	printWriter = getPrintWriter();
	Gson gson = new Gson();
	academyList= sysUserService.findAllAcademy();
	String jsonData=gson.toJson(SerMajor);
	printWriter.write(jsonData);
	printWriter.flush();
	//System.out.print("联动拿到数据了");
	return null;
	}


/**
 *异步请求过专业的Id查询班级
 * @author 黄森南
 * @date 2018-1-25
 * @return
 */
public String ClasstoJson()
{
	
	 SerCalss =sysUserService.findByMajorId(sysUser.getUserMajor());
    printWriter =getPrintWriter();
     Gson  gson =new Gson();
     String data =gson.toJson(SerCalss);
      printWriter.write(data);
     printWriter.flush();
	  return null;
}
 


	
	

	
	/**
	 * 打开列表页面
	 * @return
	 */
	public String openList(){
		 //判断专业是否为空
		if(null!=sysUser.getUserCollege()&&!("".equals(sysUser.getUserCollege())))
		 {
			academy= sysUserService.findAcayId(sysUser.getUserCollege());
		   sysUser.setUserCollege(academy.getAcademyName());
		 }
		//判断专业是否为空
		if(null!=sysUser.getUserMajor()&&!("".equals(sysUser.getUserMajor())))
				{
			   Major=sysUserService.findMajById(sysUser.getUserMajor());
			   sysUser.setUserMajor(Major.getMajorName());
			
				}
		

          if(null!=sysUser.getUserClass()&&!("".equals(sysUser.getUserClass())))
		{
			cla=sysUserService.findClassInfoByClassId(sysUser.getUserClass());
	   sysUser.setUserClass(cla.getClassName());
	
		}

	 pageResult=sysUserService.find(sysUser, getPage(), getRow());
	 academyList= sysUserService.findAllAcademy();
	 majorList =serviceMajorService.findAll();
	 SerCalss=sysUserService.findAllClass();
	SerStusdio =sysUserService.findStudio();
		setForwardView(LIST_JSP);
		return SUCCESS;
	}



	/**
	 * 打开修改页面
	 * @return
	 */
	public String openEdit(){
		//查询角色的所有信息
		roles = sysRoleService.getRoleAll();
		//comDistricts = comDistrictService.findComDistrictByUpid(0);
		//根据用户编号查询用户角色
		userRoles = sysUserRoleService.findByUserId(sysUser.getUserId());
		//根据用户id查询用户信息
	//	sysUser = sysUserService.findById(sysUser.getId());
		
		SerStusdio = serviceStudioService.findAll(); 
		
		sysUser=sysUserService.findByUserId(sysUser.getUserId());
		try{
			academyList = serviceAcademyService.findAll();
			if(sysUser.getUserCollege() != null && !"".equals(sysUser.getUserCollege())){
				String academyid = serviceAcademyService.findByProperty("academyName", sysUser.getUserCollege()).get(0).getAcademyId();
				SerMajor = serviceMajorService.findByProperty("academyId", academyid);
			}else{
				SerMajor = serviceMajorService.findAll();
			}
			if(sysUser.getUserMajor() != null && !"".equals(sysUser.getUserMajor())){
				String majorid = serviceMajorService.findByProperty("majorName", sysUser.getUserMajor()).get(0).getMajorId();
				SerCalss = serviceClassService.findByProperty("majorId", majorid);
			}else{
				SerCalss = serviceClassService.findAll();
			}

		}catch(Exception e){
			System.out.println(e);
		}

		
		setForwardView(EDIT_JSP);
		return SUCCESS;
	}
	
	/**
	 * 打开个人修改页面
	 * @return
	 */
	public String openEditSelf(){
		
		SerStusdio = serviceStudioService.findAll(); 
		

		sysUser=sysUserService.findByUserId(sysUser.getUserId());
		try{
			academyList = serviceAcademyService.findAll();
			if(sysUser.getUserCollege() != null && !"".equals(sysUser.getUserCollege())){
				String academyid = serviceAcademyService.findByProperty("academyName", sysUser.getUserCollege()).get(0).getAcademyId();
				SerMajor = serviceMajorService.findByProperty("academyId", academyid);
			}else{
				SerMajor = serviceMajorService.findAll();
			}
			if(sysUser.getUserMajor() != null && !"".equals(sysUser.getUserMajor())){
				String majorid = serviceMajorService.findByProperty("majorName", sysUser.getUserMajor()).get(0).getMajorId();
				SerCalss = serviceClassService.findByProperty("majorId", majorid);
			}else{
				SerCalss = serviceClassService.findAll();
			}

		}catch(Exception e){
			System.out.println(e);
		}
		setForwardView(EDIT_SELF_JSP);
		return SUCCESS;
	}
	/**
	 * 表格详情
	 * @return
	 */
	public String openDetails(){
		setForwardView(DETAILS_JSP);
		return SUCCESS;
	}
	
	public String openAdd() {
		setForwardView(ADD_JSP);
		return SUCCESS;
	}
	/**
	 * 删除用户
	 * @author 黄森南
	 * @date 2018-1-25
	 * @return
	 */
	public String delete() {
		
		sysUserService.delete(sysUser.getUserId());//通过用户名删除用户
		
		return openList();
		
		
	
		
	}
	/**
	 * 添加学生用户
	 * @author 黄森南
	 * @date 2018-1-25
	 * @return
	 */
	public String addStudent() {
		academy=sysUserService.findAcayId(sysUser.getUserCollege());
		 Major=sysUserService.findMajById(sysUser.getUserMajor());
		sysUser.setUserType(0);
		sysUser.setUserCollege(academy.getAcademyName());
		sysUser.setUserMajor(Major.getMajorName());
		sysUserService.add(sysUser);
		return openaddStu();
	}
	
	/**
	 * 添加教师用户
	 * @author 黄森南
	 * @date 2018-1-25
	 * @return
	 */
	public String addTeacher() {
		sysUser.setUserType(1);
		sysUserService.add(sysUser);
		
		//角色分配---li
		SysUserRole userRole = new SysUserRole("teacher_admin",sysUser.getUserId());
		sysUserRoleService.addUserRole(userRole);
		
		return openaddTea();
	}
	/**
	 * 修改个人资料
	 * @return
	 */
	public String edit(){
		sysUser.setUserCollege(serviceAcademyService.findByProperty("academyId", sysUser.getUserCollege()).get(0).getAcademyName());
		sysUser.setUserMajor(serviceMajorService.findByProperty("majorId", sysUser.getUserMajor()).get(0).getMajorName());
		sysUser.setUserClass(serviceClassService.findByProperty("classId", sysUser.getUserClass()).get(0).getClassName());
		if(sysUser.getUserPassword().equals("")){     //如果用户修改的密码为空，则是用户不修改密码，不用加密直接保存到数据库
			sysUser.setUserPassword(oldPassword);
		}else{
			//否则为已经修改了密码需要加密
			sysUser.setUserPassword(SystemContext.passwdEncryption(sysUser.getUserPassword()));
		}
		sysUserService.update(sysUser);
		return PersonInfo();
	}
	
	/**
	 * 更新用户信息
	 * @return
	 * @throws Exception
	 */
	public String update() throws Exception{
		sysUser.setUserCollege(serviceAcademyService.findByProperty("academyId", sysUser.getUserCollege()).get(0).getAcademyName());
		sysUser.setUserMajor(serviceMajorService.findByProperty("majorId", sysUser.getUserMajor()).get(0).getMajorName());
		sysUser.setUserClass(serviceClassService.findByProperty("classId", sysUser.getUserClass()).get(0).getClassName());
		sysUserService.update(sysUser,roleId, oldPassword);
		sysUser = new SysUser();
		return openList();
	}
	
	/**
	 * 查找所用用户
	 * @author 黄灵燕
	 * @date 2018-1-18
	 * @return
	 */
	public String QueryAllUser(){		
		SysUserList = sysUserService.findAll();	
		return Text();
	}
		
	public String Text(){
		setForwardView(AllUser_JSP);
		return SUCCESS;
	}
	
	
	public void Querydata() throws IOException{
//			academy=sysUserService.findAcayId(AcaVar);
//			Major=sysUserService.findMajById(MajVar);
//		    cla=sysUserService.findClassInfoByClassId(ClaVar);
//		    studio = sysUserService.findStudioByStudioId(StudioVar);
		    
		    String academyName = null;
		    String majorName = null;
		    String className = null;
		    String studioName = null;
		    
		    if(null != AcaVar && !"".equals(AcaVar)){
		    	academyName = sysUserService.findAcayId(AcaVar).getAcademyName();
		    }
		    if(null != MajVar && !"".equals(MajVar)){
		    	majorName = sysUserService.findMajById(MajVar).getMajorName();
		    }
		    if(null != ClaVar && !"".equals(ClaVar)){
		    	className = sysUserService.findClassInfoByClassId(ClaVar).getClassName();
		    }
		    if(null != StudioVar && !"".equals(StudioVar)){
		    	studioName = sysUserService.findStudioByStudioId(StudioVar).getStudioName();
		    }
		    
		    
		    
		    
		//	SysUserList = sysUserService.findSysUser(academy.getAcademyName(), Major.getMajorName(), cla.getClassName(),studio.getStudioName());	
		    SysUserList = sysUserService.findSysUser(academyName, majorName, className, studioName);
			Gson gson=new Gson();
			printWriter=getPrintWriter();
			printWriter.write(gson.toJson(SysUserList));
			printWriter.close();
			//return SUCCESS;
	}
	
	public void Submitdata(){
		String userId = getParameters("userid");
	}
	/**
	 * 测试多选
	 * @return
	 */
	public String Select(){
		 String chk_value = getParameters("chk_value");		 
	 	 String[] valueid = chk_value.split(",");
	 	for (int i = 0; i < valueid.length; i++) {
 			int Vid  = Integer.valueOf(valueid[i]);			
 		}
		return SUCCESS;
	}
	
	
	/**
	 * 查看个人信息
	 * @author 黄森南
	 * @date 2018-02-09
	 * @return
	 */
	
	public String PersonInfo(){
		sysUser = sysUserService.findByUserId((String)getRequest().getSession().getAttribute("userId"));
		if(sysUser != null){
		}
		forwardView=INFO_JSP;
		return SUCCESS;
	}
	
	/**
	 * 更新个人信息
	 * @author 黄森南
	 * @date 2018-02-09
	 * @return
	 */
	public String UpdateInfo()
	{
		
		try{
		sysUserService.update(sysUser);
		}catch(Exception e)
		{
			msg="error";
			e.printStackTrace();
			
		}
		msg="success";
		
		return openEdit();
	}
	
	
	public SysUser getSysUser() {
		return sysUser;
	}

	public void setSysUser(SysUser sysUser) {
		this.sysUser = sysUser;
	}

	/*public Result<SysUser> getPageResult() {
		return pageResult;
	}

	public void setPageResult(Result<SysUser> pageResult) {
		this.pageResult = pageResult;
	}*/

	public List<ServiceField> getFieldList() {
		return fieldList;
	}

	public void setFieldList(List<ServiceField> fieldList) {
		this.fieldList = fieldList;
	}

	public List<ServiceAcademy> getAcademyList() {
		return academyList;
	}

	public void setAcademyList(List<ServiceAcademy> academyList) {
		this.academyList = academyList;
	}

	public List<ServiceMajor> getSerMajor() {
		return SerMajor;
	}

	public void setSerMajor(List<ServiceMajor> serMajor) {
		SerMajor = serMajor;
	}

	public List<ServiceClass> getSerCalss() {
		return SerCalss;
	}

	public void setSerCalss(List<ServiceClass> serCalss) {
		SerCalss = serCalss;
	}

	public List<ServiceStudio> getSerStusdio() {
		return SerStusdio;
	}

	public void setSerStusdio(List<ServiceStudio> serStusdio) {
		SerStusdio = serStusdio;
	}

	public Result getPageResult() {
		return pageResult;
	}

	public void setPageResult(Result pageResult) {
		this.pageResult = pageResult;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public ServiceAcademy getAcademy() {
		return academy;
	}

	public void setAcademy(ServiceAcademy academy) {
		this.academy = academy;
	}

	public ServiceMajor getMajor() {
		return Major;
	}

	public void setMajor(ServiceMajor major) {
		Major = major;
	}

	public List<SysRole> getRoles() {
		return roles;
	}

	public void setRoles(List<SysRole> roles) {
		this.roles = roles;
	}

	public List<SysUserRole> getUserRoles() {
		return userRoles;
	}

	public void setUserRoles(List<SysUserRole> userRoles) {
		this.userRoles = userRoles;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public SysUserRole getSysUserRole() {
		return sysUserRole;
	}

	public void setSysUserRole(SysUserRole sysUserRole) {
		this.sysUserRole = sysUserRole;
	}

	public String[] getRoleId() {
		return roleId;
	}

	public void setRoleId(String[] roleId) {
		this.roleId = roleId;
	}

	public List<SysUser> getSysUserList() {
		return SysUserList;
	}

	public void setSysUserList(List<SysUser> sysUserList) {
		SysUserList = sysUserList;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}
	


	
}
